// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/base/api/EmergencyGroupApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/base/model/QueryEmergencyGroupListReq'
import OneClass from '@api/base/model/QueryEmergencyGroupDetailReq'
import AddClass from '@api/base/model/InsertEmergencyGroupReq'
import EditClass from '@api/base/model/UpdateEmergencyGroupReq'
import DelClass from '@api/base/model/DeleteEmergencyGroupReq'
import BaseClass from '@api/base/model/EmergencyGroupDto'
import ChildDTO from '@api/base/model/EmergencyMemberDto'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const BASE = [BaseClass, 'emergencyGroupDto']
const CHILDREN = [ChildDTO, 'emergencyMemberDto', 1]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM, BASE)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryEmergencyGroupList', DTO)
    return [resp.comQuery.totalCount, resp.emergencyGroupDto || []]
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id }, TIP = '查询详情') {
    const DTO = CORE.grpcDTO(OneClass, { id })
    const resp = await CORE.grpc(TIP, CLIENT, 'queryEmergencyGroupDetail', DTO)
    return { ...resp.emergencyGroupDto, emergencyMemberDto: resp.emergencyMemberDto }
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增') {
    const DTO = CORE.grpcDTO(AddClass, one, BASE, CHILDREN)
    await CORE.grpc(TIP, CLIENT, 'insertEmergencyGroup', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE, CHILDREN)
    await CORE.grpc(TIP, CLIENT, 'updateEmergencyGroup', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { ids: id })
    await CORE.grpc(TIP, CLIENT, 'deleteEmergencyGroup', DTO)
    return CORE.grpcOK(TIP)
  },
}
